package datastructures.list.singletrack;

import java.util.Collections;

/**
 * @author hck
 */
public class TestSingleList {
    public static void main(String[] args) {
        SingleTrackLinkedList<Integer> list = new SingleTrackLinkedList();

        list.add(0);
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        list.add(8);

        list.print();

        System.out.println("查询第三个节点");
        System.out.println(list.get(3));

        System.out.println("查询倒数第n个节点:");
        for (int backIndex = 0; backIndex < 7; backIndex++) {
            testBackIndex(list, backIndex);
        }

        System.out.println("移除第五个节点");
        System.out.println(list.remove(5));
        list.print();

        System.out.println("反转链表:");
        list.reverse();
        list.print();

        System.out.println("反向打印链表:");
        list.reversePrint();
    }

    private static void testBackIndex(SingleTrackLinkedList<Integer> list, int backIndex) {
        System.out.println("输入:" + backIndex);
        System.out.println("输出:" + getBackIndexNode(backIndex, list));
        System.out.println("-----------");
    }

    /**
     * 查询倒数第N个节点
     *
     * @param index index
     * @param list  list
     * @return
     */
    private static Integer getBackIndexNode(int index, SingleTrackLinkedList<Integer> list) {
        if (list.size() == 0) {
            return null;
        }
        if (index < 0 || index > list.size()) {
            return null;
        }

        int cursor = list.size() - index - 1;

        return list.get(cursor);
    }
}
